import properties.PET;
import user.Tutor;


class InstallController {
	
	def userService
	
	def beforeInterceptor = [action:this.&isNotConfigured]
    
    def isNotConfigured = {
		return PET.count() == 0 || Tutor.count() == 0
	}
	
    def index = {
    	redirect(action: "step1")
    }
    
    def step1 = {
		def petInstance
		if (PET.count() == 0) {
	    	if (request.post) {
	    		petInstance = new PET(params)
				if (petInstance.validate()) {
					petInstance.save(failOnError:true, flush: true)
		    		redirect(action: "step2")
		    	}
	    	}
		} else {
			redirect(action: "step2")
		}

		[petInstance: petInstance]
    }
    
    def step2 = {
		def tutorInstance
		if (Tutor.count() == 0) {
	    	if (request.post) {
	    		tutorInstance = new Tutor(params)
				if (tutorInstance.validate()) {
					tutorInstance.isAdmin = true
					tutorInstance.password = tutorInstance.password.encodeAsHash()
					userService.save(tutorInstance)
		    		redirect(controller: "index")
		    	}
			}
		} else {
			redirect(controller: "index")
		}
		if (PET.count() == 0) {
			redirect(action: "step1")
		}

		[tutorInstance: tutorInstance]
    }
}
